<?php

namespace app\reserve\model;

use think\Model;

class AdminGroup extends Model
{
    protected $table = 'orange_admin_group';

    protected $autoWriteTimestamp = true;

    protected $createTime = 'ctime';

    protected $updateTime = 'utime';

    protected $insert = ['status' => 0];

    protected $dateFormat = 'Y-m-d H:i:s';


    /**
     * @param $where
     * @param $page
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhenhong~
     * 查询权限组列表
     */
    public function getAuthGroupList($where, $page,$bool)
    {
        if($bool === true){
            $list = self::all(function($query){
                $query->field('group_id,group_name');
            });
        }else{
            $list['list'] = self::select(function ($query) use ($where, $page) {
                $query->where($where)->page($page, 10);
            });
            $list['total'] = self::where($where)->count();
        }
        return $list;
    }

    /**
     * @param $post
     * @return false|int
     * @author zhenhong~
     * 新增权限分组
     */
    public function addAuthGroup($post)
    {
        return self::save($post);
    }

    /**
     * @param $post
     * @return false|int
     * @author zhenhong~
     * 修改权限组
     */
    public function updateAuthGroup($post)
    {
        return self::isUpdate(true)->save($post);
    }

    /**
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhenhong~
     * 查看权限组
     */
    public function getDetailsById($id)
    {
        $info = self::get($id);
        $arrId = json_decode($info['permission'], true);
        $list['group_id'] = $info['group_id'];
        $list['group_name'] = $info['group_name'];
        $list['list'] = $this->getAuthTree($arrId);
        return $list;
    }

    /**
     * @param $post
     * @return array
     * 禁用-启用权限组
     * @author zhenhong~
     */
    public function setAuthStatus($post)
    {
        $msg = $status = '';
        switch ($post['status']) {
            case '0':
                $status = '1';
                $msg = '禁用成功';
                break;
            case '1':
                $status = '0';
                $msg = '启用成功';
                break;
        }
        return [
            'result' => self::where('group_id', $post['group_id'])->setField('status', $status),
            'msg' => $msg
        ];
    }

    /**
     * @param array $arrId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhenhong~
     * 根据权限组id，获取权限
     */
    public function getAuthTree(array $arrId)
    {
        $actionList = AdminAction::whereIn('action_id', $arrId)
            ->where('status','neq',9)
            ->field('action_id,action_name,fid')
            ->order('fid asc')
            ->select();
        $lefts = [];
        foreach ($actionList as $key => $value) {
            if ($value['fid'] > 0) {
                $lefts[$value['fid']]['children'][] = $value;
            } else {
                $lefts[$value['action_id']]['id'] = $value['action_id'];
                $lefts[$value['action_id']]['label'] = $value['action_name'];
            }
        }
        $lefts = array_values($lefts);
        foreach ($lefts as $k => $v) {
            if (array_key_exists('children', $v)) {
                foreach ($v['children'] as $kk => $item) {
                    $v['children'][$kk]['id'] = $item['action_id'];
                    $v['children'][$kk]['label'] = $item['action_name'];
                    unset($item['action_id'], $item['action_name'], $item['fid']);
                }
            }
        }
        return $lefts;
    }
}